package io.renren.modules.app.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.CategoryVo;
import io.renren.modules.app.entity.ChannelVo;
import io.renren.modules.app.service.ApiCategoryService;
import io.renren.modules.app.service.ApiChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 导航菜单api
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019-05-8 12:27
 */
@Api("导航菜单api")
@RestController
@RequestMapping("/app/v1/channel")
public class ApiChannelController {
    @Autowired
    private ApiChannelService channelService;
    @Autowired
    private ApiCategoryService categoryService;


    /**
     * app轮播广告
     */
    @ApiOperation("导航菜单")
    @GetMapping("list")
    public R list() {
        List<ChannelVo> channel = channelService.list(
                new QueryWrapper<ChannelVo>().eq("is_show", 1).orderByAsc("sort_order")
        );
        for (int i = 0; i < channel.size(); i++) {
            CategoryVo category = categoryService.getById(channel.get(i).getCategoryId());
            if(category.getIsShow()!=1){
                channel.get(i).setIsShow(0);
                continue;
            }
            List<CategoryVo> categoryEntityList = categoryService.list(
                    new QueryWrapper<CategoryVo>()
                            .eq("parent_id",channel.get(i).getCategoryId())
                            .eq("is_show",1)
                            .ne("name","推荐")
                            .orderByAsc("sort_order")
            );
            if( categoryEntityList.size()>0 ){
                channel.get(i).setCategoryId(categoryEntityList.get(0).getId());
            } else {
                channel.get(i).setIsShow(0);
            }
        }
        return R.ok().put("data", channel);
    }
}
